package repository

import (
	"context"
	article2 "jk-time/webook/internal/dao/article"
	"jk-time/webook/internal/domain"
)

// 演示在service层面上分流
type ArticleAuthorRepository interface {
	Create(ctx context.Context, art domain.Article) (int64, error)
	Update(ctx context.Context, art domain.Article) error
}

// CachedArticleAuthorRepository 按照道理，这里也是可以搞缓存的
type CachedArticleAuthorRepository struct {
	dao article2.ArticleAuthorDAO
}

func NewArticleAuthorRepository(dao article2.ArticleAuthorDAO) ArticleAuthorRepository {
	return &CachedArticleAuthorRepository{
		dao: dao,
	}
}
func (c *CachedArticleAuthorRepository) Create(ctx context.Context, art domain.Article) (int64, error) {
	return c.dao.Create(ctx, c.toEntity(art))
}

func (c *CachedArticleAuthorRepository) Update(ctx context.Context, art domain.Article) error {
	return c.dao.UpdateById(ctx, c.toEntity(art))
}
func (c *CachedArticleAuthorRepository) toEntity(art domain.Article) article2.Article {
	return article2.Article{
		Id:       art.Id,
		Title:    art.Title,
		Content:  art.Content,
		AuthorId: art.Author.Id,
	}
}
